﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace AssemblyInfo
{
    public partial class Form1 : Form
    {
        private string initPath;

        public Form1()
        {
            InitializeComponent();
        }

        public Form1(string initPath)
            : this()
        {
            this.initPath = initPath;
        }

        private void ListView1ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column != int.Parse(AssemblyListView.Tag.ToString()))
            {
                AssemblyListView.Tag = e.Column;
                AssemblyListView.Sorting = SortOrder.Ascending;
            }
            else
            {
                AssemblyListView.Sorting = AssemblyListView.Sorting == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
            }

            AssemblyListView.ListViewItemSorter = new ListViewItemComparer(e.Column, AssemblyListView.Sorting);

            AssemblyListView.Sort();
        }

        class ListViewItemComparer : IComparer
        {
            private readonly int col;
            private readonly SortOrder order;

            public ListViewItemComparer()
            {
                col = 0;
                order = SortOrder.Ascending;
            }

            public ListViewItemComparer(int column, SortOrder order)
            {
                col = column;
                this.order = order;
            }

            public int Compare(object x, object y)
            {
                int returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
                                               ((ListViewItem)y).SubItems[col].Text);

                if (order == SortOrder.Descending)
                    returnVal *= -1;

                return returnVal;
            }
        }

        private void FolderButton_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
            {
                string path = folderBrowserDialog1.SelectedPath;

                string[] files = Directory.GetFiles(path, "*.dll");

                DisplayAssemble(files);
            }
        }

        private void DisplayAssemble(params string[] files)
        {
            AssemblyListView.Items.Clear();

            AppDomainSetup ads = new AppDomainSetup
                                     {
                                         ApplicationBase = Environment.CurrentDirectory,
                                         DisallowBindingRedirects = false,
                                         DisallowCodeDownload = true,
                                         ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
                                     };
            AppDomain appDomain = AppDomain.CreateDomain("AD #2", null, ads);
            AssemblyInfoLoader assemblyInfoLoader = (AssemblyInfoLoader)appDomain.CreateInstanceAndUnwrap(Assembly.GetEntryAssembly().FullName, typeof(AssemblyInfoLoader).FullName);
            List<AssemblyInfo> assemblyInfos = assemblyInfoLoader.LoadAssemblyInfo(files);
            

            foreach (AssemblyInfo assemblyInfo in assemblyInfos)
            {
                ListViewItem item = AssemblyListView.Items.Add(new ListViewItem(new string[] {  assemblyInfo.File,
                  assemblyInfo.  IsJitTrackingEnabled ? "Debug" : "Release", 
                   assemblyInfo.   IsJitOptimized ? "Optimized" : "Not Optimized",
                   assemblyInfo.   IsIgnoreSymbolStoreSequencePoints ? "MSIL Sequencing" : "PDB Sequencing", 
                   assemblyInfo.   IsEditAndContinueEnabled ? "Edit and Continue Enabled" : "Edit and Continue Disabled" }));

                if (!assemblyInfo.IsJitOptimized || assemblyInfo.IsJitTrackingEnabled) item.ForeColor = Color.Red;
            }
            AppDomain.Unload(appDomain);
        }

        private void FileButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog(this) != DialogResult.OK) return;

            string[] files = openFileDialog1.FileNames;

            DisplayAssemble(files);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(initPath))
            {
                string[] files = Directory.GetFiles(initPath, "*.dll");

                DisplayAssemble(files);
            }
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            foreach (ListViewItem listViewItem in AssemblyListView.SelectedItems)
            {
                sb.AppendLine(listViewItem.Text);
            }

            Clipboard.SetText(sb.ToString());
        }
    }
}
